package hedron.dialog;

import static hedron.i18n.Internationalization._;
import hedron.Activator;
import hedron.Application;
import hedron.db.model.StoreName;
import java.util.ArrayList;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class StoreNameDialog extends AbstractDialog {

	List storesList;
	ArrayList<StoreName> storeNames;

	public StoreNameDialog(Shell parentShell) {
		super(parentShell);
	}

	void refresh() {
		int selection = storesList.getSelectionIndex();
		storesList.removeAll();
		storeNames = Application.STORES.getStoreNames();
		for (StoreName storeName : storeNames) {
			storesList.add(storeName.STORE_NAME);
		}
		storesList.setSelection(selection);
	}

	@Override
	public void create() {
		setDefaultImage(Activator.getImage("icons/store.png"));
		super.create();
		setTitle(_("Dialog.StoreName.Title"));
		setMessage(_("Dialog.StoreName.Message"), 0);
	}

	@Override
	void createContent(Composite parent) {

		Composite grid = new Composite(parent, 0);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

		{// set parent layout
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			grid.setLayout(layout);
		}
		{
			GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
			gd.widthHint = 360;
			gd.heightHint = 200;
			grid.setLayoutData(gd);
		}
		{
			List list = new List(grid, SWT.BORDER | SWT.V_SCROLL);
			GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
			list.setLayoutData(gd);
			storesList = list;
			storesList.add("");
			storesList.setSelection(0);
			refresh();
		}
		{
			Composite area = new Composite(grid, 0);
			area.setLayoutData(new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false));
			{// set parent layout
				GridLayout layout = new GridLayout();
				layout.numColumns = 1;
				area.setLayout(layout);
			}
			{// add
				Button button = new Button(area, SWT.PUSH);
				button.setText(_("Dialog.StoreName.Button.Add"));
				GridData gdata = new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false);
				gdata.widthHint = 100;
				button.setLayoutData(gdata);
				button.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {
						StringDialog stringDialog = new StringDialog(StoreNameDialog.this.getShell(),
								_("Dialog.StoreName.Button.Add.Text"), "");
						if (stringDialog.open() == Window.OK && stringDialog.getString().trim().length() > 0) {
							StoreName storeName = new StoreName();
							storeName.STORE_NAME = stringDialog.getString().trim();
							storeName.store();
						}
						refresh();
					}
				});
			}
			{// rename
				Button button = new Button(area, SWT.PUSH);
				button.setText(_("Dialog.StoreName.Button.Rename"));
				GridData gdata = new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false);
				gdata.widthHint = 100;
				button.setLayoutData(gdata);
				button.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {
						int selection = storesList.getSelectionIndex();
						if (selection >= 0) {
							StoreName storeName = storeNames.get(selection);
							StringDialog stringDialog = new StringDialog(StoreNameDialog.this.getShell(),
									_("Dialog.StoreName.Button.Rename.Text") + " '" + storeName.STORE_NAME + "':",
									storeName.STORE_NAME);
							if (stringDialog.open() == Window.OK && stringDialog.getString().trim().length() > 0) {
								storeName.STORE_NAME = stringDialog.getString().trim();
								storeName.store();
							}
							refresh();
						}
					}
				});
			}
		}
	}

	@Override
	void createFooter(Composite parent) {
		Composite grid = new Composite(parent, 0);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.END, true, false));

		{// set grid layout
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			grid.setLayout(layout);
		}
		{// cell 1 - empty text
			Label label = new Label(grid, 0);
			label.setText("");
			GridData gdata = new GridData(GridData.FILL, GridData.END, true, false);
			gdata.heightHint = 20;
			label.setLayoutData(gdata);
			label.setFocus();
		}
		{// ok
			Button button = new Button(grid, SWT.PUSH);
			button.setText("OK");
			GridData gdata = new GridData(GridData.END, GridData.END, false, false);
			gdata.heightHint = 20;
			gdata.widthHint = 100;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					setReturnCode(OK);
					close();
				}
			});

			// set button as default
			Shell shell = grid.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
	}
}
